﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Willson.JsbManager.Bll;
using Willson.JsbManager.Comm;
using Willson.JsbManager.BllEntity;

namespace Willson.JsbManager.UI
{
    public partial class XTInvoicePrintForm : BaseForm
    {
        private int m_year = 0;
        private int m_jd = 0;
        private int m_company_id = 0;
        private BindingList<BEInvoiceSbJDInfo> list = null;
        private BEInvoiceInfo invoice_info = null;


        public XTInvoicePrintForm()
        {
            InitializeComponent();
        }

        public override void SelectYearJD(int year, int jd)
        {
            if (year == 0 || jd == 0)
                this.Close();

            this.m_year = year;
            this.m_jd = jd;

        }

        public override void SelectCompanyId(int id)
        {
            if (id == 0) return;

            this.m_company_id = id;

            this.textBox_company_name.Text = BLLFactory<BLLQuery>.Instance.QueryCompanyInfoByID(this.m_company_id).Name;

            list = BLLFactory<BLLInvoicePrint>.Instance.Query(this.m_year, this.m_jd, this.m_company_id);

            this.dataGridView.DataSource = list;

        }

        private void XTInvoicePrintForm_Shown(object sender, EventArgs e)
        {
            SelectYearJdForm form = new SelectYearJdForm(this);
            form.ShowDialog();
        }

        private void bt_query_Click(object sender, EventArgs e)
        {
            SelectCompanyIDForm form = new SelectCompanyIDForm(this);
            form.ShowDialog();
        }

        private void bt_get_Click(object sender, EventArgs e)
        {
            if (list == null)
            {
                MessageUtil.ShowWarning("请选选择好用户单位再开票");
                return;
            }

            if (list.Count == 0)
            {
                MessageUtil.ShowWarning("请选选择好用户单位再开票");
                return;
            }

            int id = Convert.ToInt32(dataGridView.Rows[dataGridView.CurrentCell.RowIndex].Cells["id"].Value.ToString());

            try
            {
                invoice_info = BLLFactory<BLLInvoicePrint>.Instance.GetInvoiceInfo(id, this.m_company_id);
                bEInvoiceInfoBindingSource.DataSource = invoice_info;
            }
            catch
            {
                MessageUtil.ShowWarning("此用户单位没有超计划用水,无需开票!");
                return;
            }

            //if (info == null)
            //{
            //    MessageUtil.ShowWarning("此用户单位没有超计划用水,无需开票!");
            //    return;
            //}

            //this.textBox_tt.Text = info.Name_tt;
            //this.textBox_num.Text = info.Num.ToString();
            //this.textBox_money.Text = string.Format("{0:##.00}", info.Money);
            //this.textBox_all_money.Text = string.Format("{0:######.00}", info.All_money);


            this.textBox_tt.ReadOnly = false;
            this.textBox_num.ReadOnly = false;
            this.textBox_money.ReadOnly = false;
            this.textBox_all_money.ReadOnly = false;
            this.bt_print.Enabled = true;
        }

        private void bt_print_Click(object sender, EventArgs e)
        {
            if (MessageUtil.ShowYesNoAndTips("确定打印发票吗?") == DialogResult.No)
            {
                return;
            }
            
            
            if (string.IsNullOrEmpty(textBox_tt.Text))
            {
                MessageUtil.ShowError("请输入[公司台头]");
                textBox_tt.Focus();
                return;
            }
            
            int id = Convert.ToInt32(dataGridView.Rows[dataGridView.CurrentCell.RowIndex].Cells["id"].Value.ToString());
            if (!BLLFactory<BLLInvoicePrint>.Instance.ValidateAndSave(id, Convert.ToInt32(textBox_num.Text)))
            {
                MessageUtil.ShowError("开票数据有误!");
                textBox_num.Focus();
                return;
            }

            PrintDataSet ds = new PrintDataSet();

            DataRow objRow;

            objRow = ds.Tables["InvoicePrint"].NewRow();
            objRow["year"] = DateTime.Today.Year;
            objRow["month"] = DateTime.Today.Month;
            objRow["day"] = DateTime.Today.Day;
            objRow["name"] = this.textBox_tt.Text;
            objRow["num"] = textBox_num.Text;
            objRow["money"] = string.Format("{0:C2}",textBox_money.Text);
            objRow["all_money"] = string.Format("{0:C2}", textBox_all_money.Text);
            objRow["rmb"] = Rmb.CmycurD(string.Format("{0:C2}", textBox_all_money.Text));

            ds.Tables["InvoicePrint"].Rows.Add(objRow);

            XTPrintViewForm form = new XTPrintViewForm();
            form.Bind(ds, "InvoicePrint", "Willson.JsbManager.UI.InvoicePrint.rdlc", "PrintDataSet_InvoicePrint", null);
            form.ShowDialog();

            this.textBox_tt.ReadOnly = true;
            this.textBox_num.ReadOnly = true;
            this.textBox_money.ReadOnly = true;
            this.textBox_all_money.ReadOnly = true;
            this.bt_print.Enabled = false;

            this.textBox_tt.Text = string.Empty;
            this.textBox_num.Text = string.Empty;
            this.textBox_money.Text = string.Empty;
            this.textBox_all_money.Text = string.Empty;
  
        }

        private void textBox_num_Validating(object sender, CancelEventArgs e)
        {
            int result = 0;

            if (this.textBox_num.Text.Length == 0)
            {
                e.Cancel = false;
                return;
            }

            if (!int.TryParse(this.textBox_num.Text, out result))
            {
                e.Cancel = true;
            }
        }

        private void textBox_money_Validating(object sender, CancelEventArgs e)
        {
            decimal result = 0.0m;

            if (this.textBox_money.Text.Length == 0)
            {
                e.Cancel = false;
                return;
            }

            if (!decimal.TryParse(this.textBox_money.Text, out result))
            {
                e.Cancel = true;
            }
        }

        private void textBox_all_money_Validating(object sender, CancelEventArgs e)
        {
            decimal result = 0.0m;

            if (this.textBox_all_money.Text.Length == 0)
            {
                e.Cancel = false;
                return;
            }

            if (!decimal.TryParse(this.textBox_all_money.Text, out result))
            {
                e.Cancel = true;
            }
        }

        private void textBox_num_TextChanged(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox_num.Text) || string.IsNullOrEmpty(textBox_money.Text))
                return;
            
            invoice_info.All_money = Convert.ToDecimal(textBox_num.Text) * Convert.ToDecimal(textBox_money.Text);
        }
    }
}
